﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YJKCodeBase.Geometry;
using YJKCodeBase.TopologySuite;

namespace YJKRevitCode.RevitTopologySuite
{
	public class RevitPolygonFinder
	{
		public bool TryGetPolygon(List<Curve> lstInputCurves, out List<RevitPolygonDataBean> lstFindedValue, out List<RevitPolygonDataBean> lstCreatedValue, bool ifSplitInput = false)
		{
			lstFindedValue = new List<RevitPolygonDataBean>();
			lstCreatedValue = new List<RevitPolygonDataBean>();
			if (lstInputCurves == null)
			{
				return false;
			}
			double useZ = 0.0;
			if (!RevitOperationDataBean.CheckIfCurvesZSame(lstInputCurves, out useZ))
			{
				throw new ArgumentException();
			}
			this.m_Core = new HYPolygonFinder();
			List<Arc> tempLstArc = (from n in lstInputCurves
			where null != n && n is Arc
			select n as Arc).ToList<Arc>();
			List<HYMulPointXYZ> list = new List<HYMulPointXYZ>();
			HYMulPointXYZ item = null;
			foreach (Curve curve in lstInputCurves)
			{
				if (!(null == curve))
				{
					if (!GeometryTransformer.TryMakeHYMulPoint(curve, out item, true))
					{
						return false;
					}
					list.Add(item);
				}
			}
			List<HYPolygon> lstInput = null;
			List<HYPolygon> lstInput2 = null;
			return this.m_Core.TryGetPolygons(list, out lstInput, out lstInput2, ifSplitInput) && this.TransformLstPolygon(lstInput, ref lstFindedValue, useZ, tempLstArc) && this.TransformLstPolygon(lstInput2, ref lstCreatedValue, useZ, tempLstArc);
		}

		private bool TransformLstPolygon(List<HYPolygon> lstInput, ref List<RevitPolygonDataBean> lstResult, double useZ, List<Arc> tempLstArc = null)
		{
			if (lstResult == null)
			{
				lstResult = new List<RevitPolygonDataBean>();
			}
			RevitPolygonDataBean item = null;
			foreach (HYPolygon inputPolygon in lstInput)
			{
				if (!GeometryTransformer.TryMakeRevitPolygonData(inputPolygon, useZ, out item, tempLstArc))
				{
					return false;
				}
				lstResult.Add(item);
			}
			return true;
		}

		private HYPolygonFinder m_Core;
	}
}
